<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="utf-8">
    <title>编辑 & 添加</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/static/layuiadmin/layui/css/layui.css" media="all">
    <style>
        .layui-fluid {
            padding-top: 20px;
        }
    </style>
</head>
<body>
<div class="layui-fluid">
    <form action="<?=url()?>" class="layui-form" method="post" enctype="multipart/form-data" lay-filter="form">
        <input type="hidden" name="id" value="">
        <div class="layui-form-item">
            <label class="layui-form-label">名称</label>
            <div class="layui-input-block">
                <input type="text" name="name" value="" placeholder="请输入软件名称" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">下载地址</label>
            <div class="layui-input-block">
                <input type="text" name="url" value="" placeholder="请输入下载地址" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">MD5</label>
            <div class="layui-input-block">
                <input type="text" name="md5" value="" placeholder="请输入MD5" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">版本</label>
            <div class="layui-input-block">
                <input type="text" name="version" value="" placeholder="请输入版本" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">大小</label>
            <div class="layui-input-block">
                <input type="text" name="size" value="" placeholder="请输入大小（单位：字节）" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item status">
            <label class="layui-form-label">位数</label>
            <div class="layui-input-block">
                <?php foreach(\app\common\model\Software::bit_datalist as $key => $val): ?>
                <input type="radio" name="bit" value="<?=$key?>" title="<?=$val?>">
                <?php endforeach; ?>
            </div>
        </div>
        <div class="layui-form-item status">
            <label class="layui-form-label">最新</label>
            <div class="layui-input-block">
                <?php foreach(\app\common\model\Software::latest_datalist as $key => $val): ?>
                <input type="radio" name="latest" value="<?=$key?>" title="<?=$val?>">
                <?php endforeach; ?>
            </div>
        </div>
        <div class="layui-form-item status">
            <label class="layui-form-label">废弃</label>
            <div class="layui-input-block">
                <?php foreach(\app\common\model\Software::discard_datalist as $key => $val): ?>
                <input type="radio" name="discard" value="<?=$key?>" title="<?=$val?>">
                <?php endforeach; ?>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">类型</label>
            <div class="layui-input-block">
                <select name="type" lay-search>
                    <option value="">请选择软件类型</option>
                    <?php foreach(\app\common\model\Category::order('title', 'ASC')->column('title', 'name') as $key => $value): ?>
                        <option value="<?=$key?>"><?=$value?></option>
                    <?php endforeach; ?>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <textarea name="remark" class="layui-textarea" placeholder="请输入备注"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">发布日期</label>
            <div class="layui-input-block">
                <input type="text" name="release_date" value="" placeholder="请输入发布日期" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="submit" class="layui-btn" lay-submit lay-filter="*">提交</button>
            </div>
        </div>
    </form>
</div>
<script src="/static/layuiadmin/layui/layui.js"></script>
<script>
    layui.use(['jquery', 'layer', 'form', 'laydate', 'upload', 'element'], function(){
        let form = layui.form
            ,$ = layui.jquery
            ,layer = layui.layer
            ,element = layui.element
            ,laydate = layui.laydate
            ,result = <?=$result ?? '{}'?>;

        form.val('form', result);

        form.on('submit(*)', function (data) {
            let elem = $(data.elem);

            elem.addClass('layui-btn-disabled');

            fetch(data.form.action, {
                body: new FormData(data.form)
                ,method: 'POST'
                ,credentials: 'same-origin'
                ,headers: {
                    'X-Requested-With': 'XMLHttpRequest'
                }
            })
                .then(response => response.json())
                .then(function (result) {
                    elem.removeClass('layui-btn-disabled');

                    if (0 == result.code) {
                        layer.msg(result.msg, {end: () => location.reload()});
                    } else {
                        layer.alert(result.msg);
                    }
                })
                .catch(function (error) {
                    elem.removeClass('layui-btn-disabled');
                    layer.alert('发生错误:' + error);
                });

            return false;
        });

        laydate.render({
            elem: '[name="release_date"]'
            ,type: 'date'
            ,trigger: 'click'
        });
    });
</script>
</body>
</html>